<?php
/**
 * Description of PlayerSeasonMatches
 *
 * @author Laszlo.Pokoradi
 */
class PlayerSeasonMatches {
  const PLAYED_XPATH = "./played[1]";
  const MINUTES_XPATH = "./minutes[1]";

  const GOALS_XPATH = "./goals[1]";
  const ASSISTS_XPATH = "./assists[1]";
  const DEFENSIVE_XPATH = "./defensive[1]";

  const SUCCESSFULSAVES_XPATH = "./saves[1]/successful[1]";
  const ATTEMPTEDSAVES_XPATH = "./saves[1]/attempted[1]";
  const PERCENTSAVES_XPATH = "./saves[1]/percent[1]";

  private $played;
  private $minutes;
  private $goal_successfulSaves;
  private $assist_attemptedSaves;
  private $defensive_percentSaves;
  private $db;

  public function __construct($database) {
    $this->db = $database;

    $this->played = 0;
    $this->minutes = 0;
    $this->goal_successfulSaves = 0;
    $this->assist_attemptedSaves = 0;
    $this->defensive_percentSaves = 0;
  }

  public function loadFromNode(DOMElement $docNode) {
    $doc = $docNode->ownerDocument;

    $xPath = new DOMXPath($doc);

    $qResult = $xPath->query(PlayerSeasonMatches::PLAYED_XPATH,$docNode);
    $this->played = $qResult->item(0)->nodeValue;

    $qResult = $xPath->query(PlayerSeasonMatches::MINUTES_XPATH,$docNode);
    $this->minutes = $qResult->item(0)->nodeValue;

    $qResult = $xPath->query(PlayerSeasonMatches::SUCCESSFULSAVES_XPATH,$docNode);
    if ($qResult->length != 0) {
      $this->goal_successfulSaves = $qResult->item(0)->nodeValue;
    } else {
      $qResult = $xPath->query(PlayerSeasonMatches::GOALS_XPATH,$docNode);
      $this->goal_successfulSaves = $qResult->item(0)->nodeValue;
    }

    $qResult = $xPath->query(PlayerSeasonMatches::ATTEMPTEDSAVES_XPATH,$docNode);
    if ($qResult->length != 0) {
      $this->assist_attemptedSaves = $qResult->item(0)->nodeValue;
    } else {
      $qResult = $xPath->query(PlayerSeasonMatches::ASSISTS_XPATH,$docNode);
      $this->assist_attemptedSaves = $qResult->item(0)->nodeValue;
    }

    $qResult = $xPath->query(PlayerSeasonMatches::PERCENTSAVES_XPATH,$docNode);
    if ($qResult->length != 0) {
      $this->defensive_percentSaves = $qResult->item(0)->nodeValue;
    } else {
      $qResult = $xPath->query(PlayerSeasonMatches::DEFENSIVE_XPATH,$docNode);
      $this->defensive_percentSaves = $qResult->item(0)->nodeValue;
    }
  }

  public function toXHTML() {
    $xhtml = "<td>{$this->played}</td><td>{$this->goal_successfulSaves}</td><td>{$this->assist_attemptedSaves}</td><td>{$this->defensive_percentSaves}</td>";

    return $xhtml;
  }

  public function insertNewToDB($leagueMatchesId) {
    $this->db->set('leagueMatchesId',$leagueMatchesId);
    $this->db->set('played',$this->played);
    $this->db->set('minutes',$this->minutes);
    $this->db->set('goals_successfulSaves',$this->goal_successfulSaves);
    $this->db->set('assists_attemptedSaves',$this->assist_attemptedSaves);
    $this->db->set('defensive_percentSaves',$this->defensive_percentSaves);
    $query = $this->db->insert('playerseasonmatches');

    if (!$query) {
      $logger = Logger::getInstance();
      $logger->log($conn->ErrorMsg(), Logger::ERR);
    }
    return $this->db->insert_id();
  }

  public function amendOldInDB($id) {
    $this->db->where('id',$id);
    $query = $this->db->get('playerseasonmatches');

    if (!$query) {
      log_message('error','Error during playerseasonmatches amendment!');
    } else {
      $tRow = $query->row();
      $oldId = $tRow->id;
      $leagueMatchesId = $tRow->leagueMatchesId;

      if ($tRow->played!=$this->played
          || $tRow->minutes!=$this->minutes
          || $tRow->goals_successfulSaves!=$this->goal_successfulSaves
          || $tRow->assists_attemptedSaves!=$this->assist_attemptedSaves
          || $tRow->defensive_percentSaves!=$this->defensive_percentSaves) {
        $newId = $this->insertNewToDB($leagueMatchesId);

        $this->db->set('playerId', $leagueMatchesId);
        $this->db->set('table','playerseasonmatches');
        $this->db->set('oldId',$oldId);
        $this->db->set('newId',$newId);
        $this->db->set('insertTime',date('Y-m-d H:i:s'));
        $query = $this->db->insert('playerhistory');

        if (!$query) {
          log_message('error','Playerseasonmatches amend was unsuccesful!');
        } else {
          return $newId;
        }
      }
    }
    return $oldId;
  }

  public function loadFromDB(ADOConnection $conn, $seasonMatchesId) {
    $stmt = $conn->Prepare("SELECT * FROM `playerseasonmatches` WHERE `seasonMatchesId` = ?");
    $rs=$conn->Execute($stmt,array($seasonMatchesId));

    if (!$rs) {
      $logger = Logger::getInstance();
      $logger->log($conn->ErrorMsg(), Logger::ERR);
    }

    if ($rs->RecordCount()==0) {
      $logger = Logger::getInstance();
      $logger->log("PlayerSeasonMatchese not found in the database!", Logger::ERR);
    } else {
      $result = $rs->FetchRow();

      $this->played = $result['played'];
      $this->minutes = $result['minutes'];
      $this->goal_successfulSaves = $result['goals_successfulSaves'];
      $this->assist_attemptedSaves = $result['assists_attemptedSaves'];
      $this->defensive_percentSaves = $result['defensive_percentSaves'];
    }
  }
}
?>
